package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.service.exception.GuiguException;
import com.atguigu.spzx.common.service.utils.AuthContextUtil;
import com.atguigu.spzx.manager.helper.MenuHelper;
import com.atguigu.spzx.manager.mapper.SysMenuMapper;
import com.atguigu.spzx.manager.mapper.SysRoleMenuMapper;
import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.model.dto.system.AssginMenuDto;
import com.atguigu.spzx.model.entity.system.SysMenu;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Override
    @Transactional(readOnly = true)
    public List<SysMenu> findMenu() {
       List<SysMenu> list= sysMenuMapper.selectMenu();
       //通过MenuHelper类生成菜单树
        List<SysMenu> list1 = MenuHelper.buildTree(list);
        return list1;
    }

    @Override
    public void addMenu(SysMenu sysMenu) {
        sysMenuMapper.insertMenu(sysMenu);
    }

    @Override
    @Transactional(readOnly = true)
    public SysMenu getById(Long id) {
        SysMenu sysMenu=sysMenuMapper.selectById(id);
        return sysMenu;
    }

    @Override
    public void updateById(SysMenu sysMenu) {
        sysMenuMapper.updateById(sysMenu);
    }

    @Override
    public void deleteById(Long id) {
      Integer i =sysMenuMapper.selectParentIdById(id);
      if(i>0){
         throw new GuiguException(ResultCodeEnum.Is_Exit);
      }
      sysMenuMapper.deleteById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> toAssignMenu(Long id) {
        List<SysMenu> list= sysMenuMapper.selectMenu();
        //通过MenuHelper类生成菜单树
        List<SysMenu> list1 = MenuHelper.buildTree(list);
        Map<String, Object> map=new HashMap<>();
        //将所有菜单列表存入map
        map.put("sysMenuTreeList",list1);
        List<Long> menuIdList=sysRoleMenuMapper.getRoleIdList(id);
        map.put("roleMenuIds",menuIdList);
        return map;
    }

    @Override
    public void doAssign(AssginMenuDto assginMenuDto) {
        //获取角色id
        Long roleId = assginMenuDto.getRoleId();
        sysRoleMenuMapper.deleteMenuIdByRoleId(roleId);
        List<Map<String, Number>> menuIdList = assginMenuDto.getMenuIdList();
        if(menuIdList!=null){
            sysRoleMenuMapper.insetrMenuId(assginMenuDto);
        }

    }

    @Override
    @Transactional(readOnly = true)
    public List<SysMenuVo> getSysUserMenu() {
        //根据拦截器存储在线程中的用户信息获取用户id
        Long userId = AuthContextUtil.get().getId();
        //调用根据用户id查询用户菜单的方法
       List<SysMenu> list= sysRoleMenuMapper.selectMenuByUserId(userId);
        //将用户的菜单转换为菜单树
        List<SysMenu> list1 = MenuHelper.buildTree(list);
        //将集合中的SysMenu对象转换为SysMenuVo对象
        List<SysMenuVo> sysMenuVos = buildMenus(list1);
        return sysMenuVos;
    }
    // 将List<SysMenu>对象转换成List<SysMenuVo>对象
    private List<SysMenuVo> buildMenus(List<SysMenu> menus) {

        List<SysMenuVo> sysMenuVoList = new LinkedList<SysMenuVo>();
        for (SysMenu sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }
}
